<script setup>
import {onMounted, ref, toRefs, watch} from "vue";
import {findApi, NgaPhpApi} from "@ginstone/nga-api/dist/src/enums/NgaPhpApi";
import ReadLink from "@/components/common/link/ReadLink.vue";
import {parseQueryParam} from "@ginstone/common-utils/dist/src/utils/WebUtils";
import UserLink from "@/components/common/link/UserLink.vue";
import {decodeGBK} from "@ginstone/common-utils/dist/src/utils/StrUtils";
import {NgaDomain} from "@ginstone/nga-api/dist/src/enums/NgaDomain";
import UserSpeechLink from "@/components/common/link/UserSpeechLink.vue";
import ForumLink from "@/components/common/link/ForumLink.vue";
import FavorLink from "@/components/common/link/FavorLink.vue";

const props = defineProps({
  url: {type: String, required: true},
})
const {url,} = toRefs(props)

// 链接参数
const tid = ref()
const pid = ref()
const page = ref()
const authorId = ref()
const func = ref()
const uid = ref()
const username = ref()
const forumId = ref()
const recommend = ref()
const searchReply = ref()
const colTid = ref()
const favor = ref()

// 链接类型
const api = ref()

const getNumber = (map, key) => {
  if (map.has(key)) {
    return Number(map.get(key))
  }
  return undefined;
}

// 解析路径
const parse = (u) => {
  api.value = findApi(u)
  const queryParam = parseQueryParam(u);

  favor.value = getNumber(queryParam, "favor")
  tid.value = getNumber(queryParam, "tid")
  pid.value = getNumber(queryParam, "pid")
  page.value = getNumber(queryParam, "page")
  authorId.value = getNumber(queryParam, "authorid")
  uid.value = getNumber(queryParam, "uid")
  forumId.value = getNumber(queryParam, "fid")
  colTid.value = getNumber(queryParam, "stid")
  recommend.value = !!getNumber(queryParam, "recommend")
  searchReply.value = !!getNumber(queryParam, "searchpost")
  func.value = queryParam.get("func")
  username.value = queryParam.has("username") ? decodeGBK(queryParam.get("username")) : undefined
}

const getUrl = (u) => {
  if (u.startsWith("http")) {
    return u;
  }
  return 'https://' + u
}

onMounted(() => parse(url.value))

watch(url, (nv) => {
  parse(nv)
})

</script>

<template>
  <!--是站内链接-->
  <template v-if="api">
    <!--主题链接-->
    <read-link v-if="api===NgaPhpApi.read" :author-id="authorId" :page="page" :pid="pid" :tid="tid" bracket>
      <slot></slot>
    </read-link>
    <!--用户中心链接-->
    <user-link v-else-if="api===NgaPhpApi.nuke && func==='ucp'" :user-id="uid" :username="username" bracket>
      <slot></slot>
    </user-link>
    <template v-else-if="api===NgaPhpApi.thread">

      <!--用户发言链接-->
      <user-speech-link v-if="authorId" :forum-id="forumId" :page="page" :recommend="recommend" :search-reply="searchReply" :user-id="authorId"
                        bracket>
        <slot></slot>
      </user-speech-link>
      <!-- 版面/合集链接-->
      <forum-link v-else-if="!favor && (forumId || colTid)" :col-tid="colTid" :forum-id="forumId" :page="page" bracket>
        <slot></slot>
      </forum-link>
      <!--       收藏夹链接-->
      <favor-link v-else-if="favor" :id="favor" :page="page" bracket>
        <slot></slot>
      </favor-link>
      <!--      兜底渲染 -->
      <el-link v-else :href="NgaDomain.cn+url" target="_blank" type="warning">
        [&nbsp;<slot></slot>&nbsp;]
      </el-link>
    </template>

    <el-link v-else :href="NgaDomain.cn+url" target="_blank" type="warning">
      [&nbsp;<slot></slot>&nbsp;]
    </el-link>
  </template>
  <!--外链 -->
  <el-link v-else :href="getUrl(url)" target="_blank" type="warning">
    [&nbsp;<slot></slot>&nbsp;]
  </el-link>
</template>

<style scoped>

</style>